package com.cruise.game.controller.common;

import com.cruise.game.common.api.ApiResult;
import com.cruise.game.common.constants.CommonConstants;
import com.cruise.game.controller.BaseController;
import com.cruise.game.model.vo.chat.ChatMessageVO;
import com.cruise.game.model.vo.common.PageResult;
import com.cruise.game.service.common.ChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 聊天控制器
 *
 * @author cruise
 * @date 2025-02-28
 */
@Slf4j
@Api(tags = "聊天相关接口")
@RestController
@RequestMapping("/api/chat")
public class ChatController extends BaseController {

    @Resource
    private ChatService chatService;

    /**
     * 发送客服消息
     */
    @ApiOperation("发送客服消息")
    @PostMapping("/message")
    public ApiResult<Map<String, Object>> sendMessage(
            @ApiParam(value = "消息内容", required = true) @RequestParam("message") String message) {

        Long userId = getCurrentUserId();

        // 查找管理员用户，而不是假设ID为1的用户是管理员
        Long adminId = chatService.findAdminUser();
        if (adminId == null) {
            return ApiResult.fail("系统当前没有可用的客服管理员，请稍后重试");
        }

        Long messageId = chatService.sendMessage(userId, adminId, message, true);

        Map<String, Object> result = new HashMap<>();
        result.put("messageId", messageId);

        return ApiResult.success("消息已发送", result);
    }

    /**
     * 获取客服消息历史
     */
    @ApiOperation("获取客服消息历史")
    @GetMapping("/history")
    public ApiResult<PageResult<ChatMessageVO>> getChatHistory(
            @ApiParam(value = "页码", required = false, defaultValue = "1") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,

            @ApiParam(value = "每页条数", required = false, defaultValue = "20") @RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {

        // 限制最大页面大小
        if (pageSize > CommonConstants.MAX_PAGE_SIZE) {
            pageSize = CommonConstants.MAX_PAGE_SIZE;
        }

        Long userId = getCurrentUserId();

        return ApiResult.success(chatService.getChatHistory(userId, page, pageSize));
    }

    /**
     * 获取新消息
     */
    @ApiOperation("获取新消息")
    @GetMapping("/new-messages")
    public ApiResult<List<ChatMessageVO>> getNewMessages(
            @ApiParam(value = "管理员ID", required = true) @RequestParam("adminId") Long adminId,

            @ApiParam(value = "时间点", required = true) @RequestParam("afterTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime afterTime) {

        Long userId = getCurrentUserId();

        return ApiResult.success(chatService.getNewMessages(userId, adminId, afterTime));
    }

    /**
     * 获取未读消息数
     */
    @ApiOperation("获取未读消息数")
    @GetMapping("/unread-count")
    public ApiResult<Integer> getUnreadMessageCount(
            @ApiParam(value = "最后读取时间", required = true) @RequestParam("lastReadTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime lastReadTime) {

        Long userId = getCurrentUserId();

        return ApiResult.success(chatService.getUnreadMessageCount(userId, lastReadTime));
    }
}